package com.kzd.gtgame.main.mine

import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.kzd.gtgame.R
import com.kzd.gtgame.app.GTApplication
import com.kzd.gtgame.db.Download
import com.kzd.gtgame.jetpack.manager.DownloadManager
import com.kzd.gtgame.main.game.adapter.GameDownloadAdapter
import com.kzd.gtgame.main.popup.DownloadDeletePopup
import com.module.lemlin.owner.OwnerAbstractActivity
import kotlinx.android.synthetic.main.activity_mine_download.*

class MineDownloadActivity : OwnerAbstractActivity() {

    private val mGameDownloadAdapter = GameDownloadAdapter()

    private val mDownloadManager by lazy {
        DownloadManager.instance
    }

    private val mDownloadDeletePopup: DownloadDeletePopup by lazy {
        DownloadDeletePopup(context = this) { _, download ->
            mDownloadManager.deleteFile(download.localPath)
            mDownloadManager.deleteDownload(download.gameId)
        }
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_download

    override fun stateBar(): StatusBar {
        return StatusBar(view = flGameDownloadBar)
    }

    override fun initView() {


        rvGameDownloadBody.apply {
            itemAnimator?.addDuration = 0
            itemAnimator?.changeDuration = 0
            itemAnimator?.moveDuration = 0
            itemAnimator?.removeDuration = 0
        }.adapter = mGameDownloadAdapter.apply {
            setNewInstance(GTApplication.appDatabase.gameDownloadDao().loadAll())
        }

        mGameDownloadAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mGameDownloadAdapter.getItem(position)
            if (view.id == R.id.tvGameDownloadAction) {
                if (itemData.downStatus == 1 || itemData.downStatus == 3) {
                    mDownloadManager.download(
                        gameId = itemData.gameId,
                        gameName = itemData.gameName,
                        gameIcon = itemData.gameIcon,
                        downPath = itemData.downPath
                    )
                } else if (itemData.downStatus == 2) {
                    AppUtils.installApp(itemData.localPath)
                } else if (itemData.downStatus == 4) {
                    val packageName = AppUtils.getApkInfo(itemData.localPath)
                        ?.packageName ?: return@setOnItemChildClickListener
                    AppUtils.launchApp(packageName)
                } else {
                    mDownloadManager.stopDownload(itemData.gameId)
                }
            } else if (view.id == R.id.tvGameDownloadDelete) {
                mDownloadDeletePopup.setData(itemData).showPopupWindow()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mDownloadManager.initData()
    }

    override fun onEventMainThread(any: Any) {
        if (any !is Download) {
            return
        }
        val index = mGameDownloadAdapter.data
            .indexOfLast { it.gameId == any.gameDownload?.gameId }
        if (index == -1 || any.gameDownload == null)
            return
        if (any.action == Download.Action.ACTION_UPDATE) {
            mGameDownloadAdapter.setData(index, any.gameDownload)
        } else if (any.action == Download.Action.ACTION_DELETE) {
            mGameDownloadAdapter.removeAt(index)
        }
    }

}